import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-22
 * Time: 21:01
 */
class MedianFinder {
    PriorityQueue<Integer> left;
    PriorityQueue<Integer> right;
    public MedianFinder() {
        left = new PriorityQueue<>((a,b)->(b-a));
        right = new PriorityQueue<>((a,b)->(a-b));
    }

    public void addNum(int num) {
        if(left.size() == right.size()) {
            if(left.isEmpty() || num <= left.peek()) {
                left.offer(num);
            }else {
                right.offer(num);
                left.offer(right.poll());
            }
        }else {
            if(num <= left.peek()) {
                left.offer(num);
                right.offer(left.poll());
            }else {
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if(left.size() == right.size()) {
            return (left.peek()+right.peek()) / 2.0;
        }else {
            return left.peek();
        }
    }
}
